<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="折扣上限">
          <a-input-number v-decorator="[ 'discountPriceLimit', {}]" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="是否删除（0：未删除，1：已删除）">-->
<!--          <a-input placeholder="请输入是否删除（0：未删除，1：已删除）" v-decorator="['isDelete', {}]" />-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="折扣">
          <a-input placeholder="请输入折扣" v-decorator="['percentOff', {}]" />
        </a-form-item>

        <a-form-item label="优惠类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'preferentialType', {}]" placeholder="请选择优惠类型">
            <a-select-option :value="1">代金卷</a-select-option>
            <a-select-option :value="2">折扣卷</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="是否设置满减" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'priceAvailable', validatorRules.priceAvailable]" placeholder="">
            <a-select-option :value="0">不限制</a-select-option>
            <a-select-option :value="1">大于XXX金额可用</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="优惠金额">
          <a-input-number v-decorator="[ 'priceOff', {}]" />
        </a-form-item>

<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="商店">-->
<!--          <a-input placeholder="请输入商店" v-decorator="['shopId', validatorRules.shopId ]" />-->
<!--        </a-form-item>-->

        <a-form-item label="优惠码状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'status', validatorRules.status]" placeholder="请选择优惠码状态">
            <a-select-option :value="1">未使用</a-select-option>
            <a-select-option :value="2">已使用</a-select-option>
            <a-select-option :value="3">已失效</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="领取类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'takeType', validatorRules.takeType]" placeholder="请选择领取类型">
            <a-select-option :value="1">用户主动领取</a-select-option>
            <a-select-option :value="2">后台自动发放</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="优惠劵（码）分组编号">
          <a-input-number v-decorator="[ 'templateId', validatorRules.templateId ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标题">
          <a-input placeholder="请输入标题" v-decorator="['title', validatorRules.title ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="使用时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'usedTime', {}]" />
        </a-form-item>

<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="用户编号">-->
<!--          <a-input-number v-decorator="[ 'userId', validatorRules.userId ]" />-->
<!--        </a-form-item>-->

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="生效结束时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'validEndTime', validatorRules.validEndTime ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="生效开始时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'validStartTime', validatorRules.validStartTime ]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "CouponCardModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        preferentialType:{rules: [{ required: true, message: '请输入优惠类型' }]},
        priceAvailable:{rules: [{ required: true, message: '请输入是否设置满多少金额可用' }]},
        shopId:{rules: [{ required: true, message: '请输入商店!' }]},
        status:{rules: [{ required: true, message: '请输入优惠码状态' }]},
        takeType:{rules: [{ required: true, message: '请输入领取类型' }]},
        templateId:{rules: [{ required: true, message: '请输入优惠劵（码）分组编号' }]},
        title:{rules: [{ required: true, message: '请输入标题!' }]},
        userId:{rules: [{ required: true, message: '请输入用户编号!' }]},
        validEndTime:{rules: [{ required: true, message: '请输入生效结束时间!' }]},
        validStartTime:{rules: [{ required: true, message: '请输入生效开始时间!' }]},
        },
        url: {
          add: "/admins/coupon/coupon/add",
          edit: "/couponcard/couponCard/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'discountPriceLimit','isDelete','percentOff','preferentialType','priceAvailable','priceOff','shopId','status','takeType','templateId','title','userId'))
		  //时间格式化
          this.form.setFieldsValue({usedTime:this.model.usedTime?moment(this.model.usedTime):null})
          this.form.setFieldsValue({validEndTime:this.model.validEndTime?moment(this.model.validEndTime):null})
          this.form.setFieldsValue({validStartTime:this.model.validStartTime?moment(this.model.validStartTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.usedTime = formData.usedTime?formData.usedTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.validEndTime = formData.validEndTime?formData.validEndTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.validStartTime = formData.validStartTime?formData.validStartTime.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>